package popUpMenus
{	
	import adobe.utils.CustomActions;
	
	import caurina.transitions.Tweener;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	import mx.logging.LogEventLevel;
	
	import view.buttons.ButtonsBehaviors;
	
	public class PopUpMenuCreator extends MovieClip
	{
		
		private var _containerRectangle:MovieClip;
		private var _dragable:MovieClip;
		private var _blackBack:MovieClip;
		private var _thisPlace:MovieClip;
		private var _thisCaller:DisplayObjectContainer;
		private var _thisPlaceRoot:DisplayObjectContainer;
		private var _thisStage:Stage;
		private var _btBehaviors:ButtonsBehaviors;
		
		private var _timeOut:Number;
		private var _timeOutTimer:Timer;
		
		private var _componentDesign:MovieClip;
		private var _buttonDesign:MovieClip;
		private var _adjustClosePosition:Boolean = true;
		
		private var _posX:Number;
		private var _posY:Number;
		
		private var _width:Number;
		private var _height:Number;
		private var _backColor:uint=0x000000;
		private var _backAlpha:Number=.2;
		private var _containerColor:uint=0xE1E1E1;
		private var _containerAlpha:Number=1;
		private var _roundCorners:Number=5;
		private var _lineContainerColor:uint=0xFFFFFF;
		private var _lineThikness:Number=3;
		private var _lineAlpha:Number=1;
	
		private var _adjust:Boolean;
		private var _offSetContent:Number=0;
		
		private var _customContainerDesign:MovieClip;

		private static var logger:ILogger = Log.getLogger("popUpMenus");
		
		public function PopUpMenuCreator (_stage:Stage, thisCaller:*, componentDesign:MovieClip, buttonDesign:MovieClip, containerDesign:MovieClip=null, width:Number=0, height:Number=0, posX:Number=0, posY:Number=0, timeout:Number=0, adjustToContent:Boolean=false)
		{
			logger.log(LogEventLevel.INFO, "PopCreator");
			_thisStage=_stage;
			_thisCaller=thisCaller;
			_thisPlace=componentDesign;
			_componentDesign=componentDesign;
			_buttonDesign=buttonDesign;
			_customContainerDesign = containerDesign;
			if(_customContainerDesign == null){
				_width=width;
				_height=height;
			}else{
				_width=_customContainerDesign.width;
				_height=_customContainerDesign.height;
			}
			_posX:posX;
			_posY:posY;
			_timeOut=timeout;
			_adjust=adjustToContent;
			
			_timeOutTimer = new Timer(_timeOut, 1);
			_timeOutTimer.addEventListener(TimerEvent.TIMER_COMPLETE, forceHide);
		}
		
		public function createNewContainer():void {
			_blackBack= new MovieClip;
			_containerRectangle= new MovieClip;
			_dragable= new MovieClip;
			
			setBackground();
			setContainerBack();
			setComponentDesign();
			setButtonClose();
			setDragableArea();
			
			_thisCaller.parent.addChildAt(_blackBack, _thisCaller.parent.getChildIndex(_thisCaller));
			_thisCaller.addChild(_dragable);
		}
		
		private function setButtonClose():void
		{
			_btBehaviors = new ButtonsBehaviors(_buttonDesign);
			_buttonDesign.alpha=0;
			_buttonDesign.scaleX=0;
			_buttonDesign.scaleY=0;
			if(_adjustClosePosition){
				_buttonDesign.x=_width+5;
			}
			_buttonDesign.addEventListener(MouseEvent.MOUSE_DOWN, closePopUp);
		}
		
		private function setComponentDesign():void
		{
			_componentDesign.alpha=0;

		}
		
		private function setDragableArea():void
		{
			_dragable.addChild(_containerRectangle);
			_dragable.addChild(_buttonDesign);
			_dragable.addChild(_componentDesign);
			_dragable.x=(_thisStage.stageWidth/2-_width/2);
			_dragable.y=(_thisStage.stageHeight/2-_height/2);			
		}
		
		private function setContainerBack():void
		{
			_containerRectangle.graphics.clear();
			_containerRectangle.graphics.lineStyle(_lineThikness, _lineContainerColor, _lineAlpha);
			_containerRectangle.graphics.beginFill(_containerColor,_containerAlpha);
			_containerRectangle.graphics.drawRoundRect(0,0,_width,_height,_roundCorners,_roundCorners);
			_containerRectangle.graphics.endFill();
			_containerRectangle.mouseChildren=false;
			_containerRectangle.x=_width;
			_containerRectangle.y=0;
			_containerRectangle.scaleX =0;
			_containerRectangle.scaleY =0;			
		}
		
		private function setBackground():void
		{
			_blackBack.graphics.beginFill(_backColor,_backAlpha);
			_blackBack.graphics.drawRect(0,0,_thisStage.stageWidth,_thisStage.stageHeight);
			_blackBack.graphics.endFill();
			_blackBack.alpha=0;
			_blackBack.visible=false;			
		}
		
		private function closePopUp(event:MouseEvent):void {
			hidePopUp();
			removeEventListener(MouseEvent.MOUSE_DOWN, closePopUp);
		}
		
		private function dragMenu(event:MouseEvent):void {
			_dragable.startDrag();
			_dragable.addEventListener(MouseEvent.MOUSE_UP, DragStop);
			_dragable.removeEventListener(MouseEvent.MOUSE_DOWN, dragMenu);
		}
		
		private function DragStop(event:MouseEvent):void {
			_dragable.stopDrag();
			_dragable.removeEventListener(MouseEvent.MOUSE_UP, DragStop);
			_dragable.addEventListener(MouseEvent.MOUSE_DOWN, dragMenu);
		}
		
		public function showPopUp():void {
			_blackBack.visible=true;
			if(_adjust){
				setContainerBack();
			}
			Tweener.addTween(_blackBack, {alpha:1, time:1, transition:"linear"});
			Tweener.addTween(_buttonDesign, {scaleX:1, scaleY:1, time:2, alpha:1, transition:"easeOutElastic"});
			Tweener.addTween(_containerRectangle, {scaleX:1, scaleY:1, x:0 , y:0 , time:.7, transition:"easeOutSine", onComplete:showComponentDesign});
			
			function showComponentDesign():void {
				Tweener.addTween(_componentDesign, {alpha:1, time:.5, transition:"linear"});
			}
			
			_dragable.addEventListener(MouseEvent.MOUSE_DOWN, dragMenu);
			
			if (_timeOut!=0){
				_timeOutTimer.start();
			}
			
		}
		
		private function forceHide(event:TimerEvent):void {
			hidePopUp();
			removeEventListener(MouseEvent.MOUSE_DOWN, closePopUp);
			_timeOutTimer.stop();
			_timeOutTimer.reset();
		}
		
		public function hidePopUp():void {
			_dragable.removeEventListener(MouseEvent.MOUSE_DOWN, dragMenu);
			Tweener.addTween(_componentDesign, {alpha:0, time:.5, transition:"linear", onComplete:hidePop});
			
			function hidePop():void {
				Tweener.addTween(_buttonDesign, {scaleX:0, scaleY:0, time:2, alpha:0, transition:"easeOutElastic"});
				Tweener.addTween(_containerRectangle, {scaleX:0, scaleY:0, x:_width , y:0 , time:.7, transition:"easeOutSine"});
				Tweener.addTween(_blackBack, {alpha:0, time:1, transition:"linear", onComplete:disableBlack});
			}
			
			function disableBlack():void {
				_blackBack.visible=false;
			}
		}

		public function get timeOut():Number
		{
			return _timeOut;
		}

		public function set timeOut(value:Number):void
		{
			_timeOut = value;
			_timeOutTimer.delay=_timeOut;
		}

		public function get backColor():uint
		{
			return _backColor;
		}

		public function set backColor(value:uint):void
		{
			_backColor = value;
		}

		public function get backAlpha():Number
		{
			return _backAlpha;
		}

		public function set backAlpha(value:Number):void
		{
			_backAlpha = value;
		}

		public function get adjust():Boolean
		{
			return _adjust;
		}

		public function set adjust(value:Boolean):void
		{
			_adjust = value;
		}

		public function get adjustClosePosition():Boolean
		{
			return _adjustClosePosition;
		}

		public function set adjustClosePosition(value:Boolean):void
		{
			_adjustClosePosition = value;
		}

		public function get containerColor():uint
		{
			return _containerColor;
		}

		public function set containerColor(value:uint):void
		{
			_containerColor = value;
		}

		public function get containerAlpha():Number
		{
			return _containerAlpha;
		}

		public function set containerAlpha(value:Number):void
		{
			_containerAlpha = value;
		}

		public function get roundCorners():Number
		{
			return _roundCorners;
		}

		public function set roundCorners(value:Number):void
		{
			_roundCorners = value;
		}

		public function get lineContainerColor():uint
		{
			return _lineContainerColor;
		}

		public function set lineContainerColor(value:uint):void
		{
			_lineContainerColor = value;
		}

		public function get lineThikness():Number
		{
			return _lineThikness;
		}

		public function set lineThikness(value:Number):void
		{
			_lineThikness = value;
		}

		public function get lineAlpha():Number
		{
			return _lineAlpha;
		}

		public function set lineAlpha(value:Number):void
		{
			_lineAlpha = value;
		}

		public function get offSetContent():Number
		{
			return _offSetContent;
		}

		public function set offSetContent(value:Number):void
		{
			_offSetContent = value;
		}

		public function get customContainerDesign():MovieClip
		{
			return _customContainerDesign;
		}

		public function set customContainerDesign(value:MovieClip):void
		{
			_customContainerDesign = value;
		}


	}
}